import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Jingying from "../views/Home/components/Home.vue"
import Layout from "../layout/index.vue"
import Fly from "../views/Fly.vue"
import { getUserRole } from "@/api/VipApi"
import store from "@/store/index"
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/layout/jingying',
  },
  {
    path: '/layout',
    name: 'layout',
    component: Layout,
    children: [
      {
        path: 'jingying',
        name: 'jingying',
        meta: { title: "经营概况" },
        component: Jingying
      },
      {
        path: 'guahao',
        name: 'guahao',
        meta: { title: "挂号管理" },
        component: () => import("@/views/GuaHao/GuaHao.vue")
      },
      {
        path: 'tongji',
        name: 'tongji',
        meta: { title: "统计报表" },
        component: () => import("@/views/TongJi/TongJi.vue")
      },
      {
        path: 'vip',
        name: 'vip',
        meta: { title: "会员管理" },
        component: () => import("@/views/Vip/Vip.vue")
      },
      {
        path: 'xinkai',
        name: 'xinkai',
        meta: { title: "新开就诊" },
        component: () => import("@/views/XinKai/XinKai.vue")
      },
      {
        path: 'yaopin',
        name: 'yaopin',
        meta: { title: "药品零售" },
        component: () => import("@/views/YaoPin/YaoPin.vue")
      },
      {
        path: 'gongzuotai',
        name: 'gongzuotai',
        meta: { title: "工作台" },
        component: () => import("@/views/GongZuoTai/GongZuoTai.vue")
      },
      {
        path: 'shoufei',
        name: 'shoufei',
        meta: { title: "收费管理" },
        component: () => import("@/views/ShouFei/ShouFei.vue")
      },
      {
        path: 'huanzhe',
        name: 'huanzhe',
        meta: { title: "患者管理" },
        component: () => import("@/views/HuanZhe/HuanZhe.vue")
      },
      {
        path: 'yaoping',
        name: 'yaoping',
        meta: { title: "药品管理" },
        component: () => import("@/views/YaoPinG/YaoPinG.vue")
      },

      // {
      //   path: 'tongji',
      //   name: 'tongji',

      //   component: () => import("@/views/TongJi.vue")
      // },
      {
        path: 'xitong',
        name: 'xitong',
        meta: { title: "系统设置" },
        component: () => import("@/views/XiTong/XiTong.vue")
      },

    ]
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue")
  },
  {
    path: "/zhuce",
    name: "zhuce",
    component: () => import("@/views/ZhuCe/index.vue")
  }, {
    path: "/findpassword",
    name: "findpassword",
    component: () => import("@/views/FindPassword/index.vue")
  }



]

const otherPerson = [
  {
    path: "admin",
    name: "admin",
    meta: { title: "管理", roles: ["管理", "用户"] },
    component: () => import("@/views/Admin/index.vue")
  },
  {
    path: "user",
    name: "user",
    meta: { title: "用户", roles: ["管理"] },
    component: () => import("@/views/User/index.vue")
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

interface State {
  [propName: string]: any
}
const activeAddRoute = (role: any, routeArr: any) => {

  let childrenArr = router.options.routes[1].children
  let arr: any = []
  routeArr.forEach((item: any) => {

    if (item.meta.roles.includes(role)) {
      arr.push(item)
      router.addRoute("layout", item)
    }
  })
  let concatArr = childrenArr?.concat(arr)
  // localStorage.setItem("menus", JSON.stringify(concatArr))
  return concatArr
}

const whiteArr = ["/login", "/zhuce", "/findpassword"]
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token")
  let path = to.path
  if (token) {    //如果有token，在跳转login，无需再次登录，直接进首页
    if (path == "/login") {
      next("/")
    } else {
      getUserRole(token).then((res: any) => {
        let role = res.data.role
        if (store.state.menus.length == 0) {
          let resArr = activeAddRoute(role, otherPerson)
          store.dispatch("ConcatMenuArrAsync", resArr).then(() => {

            next({ ...to, replace: true })
          })
        }
        next()

      })
    }
  } else {
    if (whiteArr.includes(path)) {  //没有token的情况，如果是找回密码、登录和注册跳转到响应的页面
      next()
    } else {   //如果不是跳转到登录页
      next("/login")
    }
  }
})
export default router
